<template>
  <div class="employees-container">
    <div class="app-container">
      <PageTools>
        <template #left>
          <div class="tips">
            <i class="el-icon-info" />
            <span>总记录数: {{ total }} 条</span>
          </div>
        </template>
        <template #right>
          <el-button
            type="warning"
            size="small"
            @click="$router.push('/uploadExcel')"
          >excel导入</el-button>
          <el-button
            type="danger"
            size="small"
            @click="importExcel"
          >excel导出</el-button>
          <el-button
            type="primary"
            size="small"
            @click="showDialog = true"
          >新增员工</el-button>
        </template>
      </PageTools>
      <el-card style="margin-top: 10px">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" width="80" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" width="120">
            <template #default="{row}">
              <!-- <img :src="scope.row.staffPhoto" alt="" width="80"> -->
              <el-image
                :src="row.staffPhoto"
                class="avatar"
              >
                <!-- 具名插槽新语法  图片加载出错的时候自动显示 -->
                <!-- <template #error> -->
                <!-- <i class="el-icon-picture-outline" /> -->
                <!-- <img src="@/assets/common/bigUserHeader.png" width="80"> -->
                <!-- </template> -->
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{ row }">
              {{ formOf(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间">
            <template #default="{ row }">
              {{ formatDate(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{ row }">
              <el-button
                type="text"
                size="small"
                @click="goDetail(row.id)"
              >查看</el-button>
              <el-button
                type="text"
                size="small"
                @click="assignRole(row.id)"
              >分配角色</el-button>
              <el-button
                type="text"
                size="small"
                @click="delEmployee(row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            layout="prev, pager, next"
            :total="total"
            :page-size="params.size"
            @current-change="currentPage"
          />
        </div>
      </el-card>
    </div>
    <!-- 新增弹窗组件 -->
    <add-employee
      :show-dialog="showDialog"
      @close-dialog="closeDialog"
      @updata-list="hgetEmployeeList"
    />
    <!-- 分配弹窗 -->
    <assign-role :show-role-dialog="showRoleDialog" :cur-id="curId" @close-dialog="closeDialog" />
  </div>
</template>

<script>
import { GetEmployeeList, delEmployee } from '@/api/employee'
import AddEmployee from './components/add-employee.vue'
import AssignRole from './components/assign-role.vue'
import dayjs from 'dayjs'
import { getExportData } from '@/utils/excelData'
export default {
  components: {
    AddEmployee,
    AssignRole
  },
  data() {
    return {
      list: [],
      total: 0,
      params: {
        page: 1,
        size: 5
      },
      showDialog: false,
      showRoleDialog: false,
      curId: null
    }
  },
  mounted() {
    this.hgetEmployeeList()
  },
  methods: {
    // 获取员工列表
    async hgetEmployeeList() {
      const res = await GetEmployeeList(this.params)
      const { rows, total } = res
      this.list = rows
      this.total = total
    },
    // 1 为 显示正式  2  为 显示 非正式
    formOf(type) {
      const map = {
        1: '正式',
        2: '非正式'
      }
      return map[type]
    },
    // 切换页码 刷新列表
    currentPage(p) {
      this.params.page = p
      this.hgetEmployeeList()
    },
    // 删除员工
    delEmployee(val) {
      this.$confirm('是否删除该员工', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          // 后台删除
          await delEmployee(val)
          // 提示
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          // 再次获取列表 刷新
          this.hgetEmployeeList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭弹窗
    closeDialog() {
      this.showDialog = false
      this.showRoleDialog = false
    },
    //  时间格式化
    formatDate(value, str = 'YYYY-MM-DD') {
      return dayjs(value).format(str)
    },
    // 导出excel
    async importExcel() {
      const res = await GetEmployeeList(this.params)
      import('@/vendor/Export2Excel').then((excel) => {
        // excel表示导入的模块对象
        // 1.准备表头中的中英文对应关系
        const headerRelation = {
          手机号: 'mobile',
          姓名: 'username',
          入职日期: 'timeOfEntry',
          工号: 'workNumber',
          转正日期: 'correctionTime',
          部门: 'departmentName'
        }
        const { header, data } = getExportData(res.rows, headerRelation)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    // 查看员工详情
    goDetail(id) {
      this.$router.push({
        path: '/employees/employeesDetail',
        query: {
          id
        }
      })
    },
    // 角色分配
    assignRole(id) {
      this.showRoleDialog = true
      this.curId = id
    }
  }
}
</script>

<style lang="scss" scoped>
.tips {
  line-height: 34px;
  padding: 0px 15px;
  border-radius: 5px;
  border: 1px solid rgba(145, 213, 255, 1);
  background: rgba(230, 247, 255, 1);
  i {
    margin-right: 10px;
    color: #409eff;
  }
}
.avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
}
</style>
